{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "from textwrap import dedent\n",
    "\n",
    "import pytablewriter\n",
    "\n",
    "table_name = \"example_table\"\n",
    "header_list = [\"int\", \"float\", \"str\", \"bool\", \"mix\", \"time\"]\n",
    "data = [\n",
    "    [0, 0.1, \"hoge\", True, 0, \"2017-01-01 03:04:05+0900\"],\n",
    "    [2, \"-2.23\", \"foo\", False, None, \"2017-12-23 12:34:51+0900\"],\n",
    "    [3, 0, \"bar\", \"true\", \"inf\", \"2017-03-03 22:44:55+0900\"],\n",
    "    [-10, -9.9, \"\", \"FALSE\", \"nan\", \"2017-01-01 00:00:00+0900\"],\n",
    "]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "csv\n",
      "elasticsearch\n",
      "excel\n",
      "htm\n",
      "html\n",
      "javascript\n",
      "js\n",
      "json\n",
      "latex_matrix\n",
      "latex_table\n",
      "ltsv\n",
      "markdown\n",
      "md\n",
      "mediawiki\n",
      "null\n",
      "numpy\n",
      "pandas\n",
      "py\n",
      "python\n",
      "rst\n",
      "rst_csv_table\n",
      "rst_grid_table\n",
      "rst_simple_table\n",
      "space_aligned\n",
      "sqlite\n",
      "toml\n",
      "tsv\n"
     ]
    }
   ],
   "source": [
    "for name in pytablewriter.TableWriterFactory.get_format_name_list():\n",
    "    print(name)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "csv\n",
      "htm\n",
      "html\n",
      "js\n",
      "json\n",
      "ltsv\n",
      "md\n",
      "py\n",
      "rst\n",
      "sqlite\n",
      "sqlite3\n",
      "tex\n",
      "toml\n",
      "tsv\n",
      "xls\n",
      "xlsx\n"
     ]
    }
   ],
   "source": [
    "for name in pytablewriter.TableWriterFactory.get_extension_list():\n",
    "    print(name)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\"int\",\"float\",\"str\",\"bool\",\"mix\",\"time\"\n",
      "0,0.1,\"hoge\",True,0,\"2017-01-01 03:04:05+0900\"\n",
      "2,-2.23,\"foo\",False,,\"2017-12-23 12:34:51+0900\"\n",
      "3,0,\"bar\",True,Infinity,\"2017-03-03 22:44:55+0900\"\n",
      "-10,-9.9,,False,NaN,\"2017-01-01 00:00:00+0900\"\n"
     ]
    }
   ],
   "source": [
    "writer = pytablewriter.CsvTableWriter()\n",
    "writer.header_list = header_list\n",
    "writer.value_matrix = data\n",
    "\n",
    "writer.write_table()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      " PID   USER  PR  NI    VIRT   RES   SHR   S  %CPU  %MEM   TIME+     COMMAND   \n",
      "32866  root  20    0   48344  3924  3448  R   5.6   0.2  0:00.03  top         \n",
      "    1  root  20    0  212080  7676  5876  S     0   0.4  1:06.56  systemd     \n",
      "    2  root  20    0       0     0     0  S     0     0  0:01.92  kthreadd    \n",
      "    4  root   0  -20       0     0     0  S     0     0  0:00.00  kworker/0:0H\n"
     ]
    }
   ],
   "source": [
    "writer = pytablewriter.SpaceAlignedTableWriter()\n",
    "writer.header_list = [\n",
    "    \"PID\",\n",
    "    \"USER\",\n",
    "    \"PR\",\n",
    "    \"NI\",\n",
    "    \"VIRT\",\n",
    "    \"RES\",\n",
    "    \"SHR\",\n",
    "    \"S\",\n",
    "    \"%CPU\",\n",
    "    \"%MEM\",\n",
    "    \"TIME+\",\n",
    "    \"COMMAND\",\n",
    "]\n",
    "writer.value_matrix = csv1 = [\n",
    "    [32866, \"root\", 20, 0, 48344, 3924, 3448, \"R\", 5.6, 0.2, \"0:00.03\", \"top\"],\n",
    "    [1, \"root\", 20, 0, 212080, 7676, 5876, \"S\", 0, 0.4, \"1:06.56\", \"systemd\"],\n",
    "    [2, \"root\", 20, 0, 0, 0, 0, \"S\", 0, 0, \"0:01.92\", \"kthreadd\"],\n",
    "    [4, \"root\", 0, -20, 0, 0, 0, \"S\", 0, 0, \"0:00.00\", \"kworker/0:0H\"],\n",
    "]\n",
    "\n",
    "writer.write_table()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\"int\"\t\"float\"\t\"str\"\t\"bool\"\t\"mix\"\t\"time\"\n",
      "0\t0.1\t\"hoge\"\tTrue\t0\t\"2017-01-01 03:04:05+0900\"\n",
      "2\t-2.23\t\"foo\"\tFalse\t\t\"2017-12-23 12:34:51+0900\"\n",
      "3\t0\t\"bar\"\tTrue\tInfinity\t\"2017-03-03 22:44:55+0900\"\n",
      "-10\t-9.9\t\tFalse\tNaN\t\"2017-01-01 00:00:00+0900\"\n"
     ]
    }
   ],
   "source": [
    "writer = pytablewriter.TsvTableWriter()\n",
    "writer.header_list = header_list\n",
    "writer.value_matrix = data\n",
    "\n",
    "writer.write_table()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<table id=\"example_table\">\n",
      "    <caption>example_table</caption>\n",
      "    <thead>\n",
      "        <tr>\n",
      "            <th>int</th>\n",
      "            <th>float</th>\n",
      "            <th>str</th>\n",
      "            <th>bool</th>\n",
      "            <th>mix</th>\n",
      "            <th>time</th>\n",
      "        </tr>\n",
      "    </thead>\n",
      "    <tbody>\n",
      "        <tr>\n",
      "            <td align=\"right\">0</td>\n",
      "            <td align=\"right\">0.10</td>\n",
      "            <td align=\"left\">hoge</td>\n",
      "            <td align=\"left\">True</td>\n",
      "            <td align=\"right\">0</td>\n",
      "            <td align=\"left\">2017-01-01 03:04:05+0900</td>\n",
      "        </tr>\n",
      "        <tr>\n",
      "            <td align=\"right\">2</td>\n",
      "            <td align=\"right\">-2.23</td>\n",
      "            <td align=\"left\">foo</td>\n",
      "            <td align=\"left\">False</td>\n",
      "            <td align=\"left\"></td>\n",
      "            <td align=\"left\">2017-12-23 12:34:51+0900</td>\n",
      "        </tr>\n",
      "        <tr>\n",
      "            <td align=\"right\">3</td>\n",
      "            <td align=\"right\">0.00</td>\n",
      "            <td align=\"left\">bar</td>\n",
      "            <td align=\"left\">True</td>\n",
      "            <td align=\"left\">Infinity</td>\n",
      "            <td align=\"left\">2017-03-03 22:44:55+0900</td>\n",
      "        </tr>\n",
      "        <tr>\n",
      "            <td align=\"right\">-10</td>\n",
      "            <td align=\"right\">-9.90</td>\n",
      "            <td align=\"left\"></td>\n",
      "            <td align=\"left\">False</td>\n",
      "            <td align=\"left\">NaN</td>\n",
      "            <td align=\"left\">2017-01-01 00:00:00+0900</td>\n",
      "        </tr>\n",
      "    </tbody>\n",
      "</table>\n"
     ]
    }
   ],
   "source": [
    "writer = pytablewriter.HtmlTableWriter()\n",
    "writer.table_name = table_name\n",
    "writer.header_list = header_list\n",
    "writer.value_matrix = data\n",
    "\n",
    "writer.write_table()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "const example_table = [\n",
      "    [\"int\", \"float\", \"str\", \"bool\", \"mix\", \"time\"],\n",
      "    [0, 0.1, \"hoge\", true, 0, \"2017-01-01 03:04:05+0900\"],\n",
      "    [2, -2.23, \"foo\", false, null, \"2017-12-23 12:34:51+0900\"],\n",
      "    [3, 0, \"bar\", \"true\", Infinity, \"2017-03-03 22:44:55+0900\"],\n",
      "    [-10, -9.9, \"\", \"FALSE\", NaN, \"2017-01-01 00:00:00+0900\"]\n",
      "];\n",
      "\n"
     ]
    }
   ],
   "source": [
    "writer = pytablewriter.JavaScriptTableWriter()\n",
    "writer.table_name = table_name\n",
    "writer.header_list = header_list\n",
    "writer.value_matrix = data\n",
    "\n",
    "writer.write_table()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[\n",
      "{\n",
      "    \"bool\": true,\n",
      "    \"float\": 0.1,\n",
      "    \"int\": 0,\n",
      "    \"mix\": 0,\n",
      "    \"str\": \"hoge\",\n",
      "    \"time\": \"2017-01-01 03:04:05+0900\"\n",
      "},\n",
      "{\n",
      "    \"bool\": false,\n",
      "    \"float\": -2.23,\n",
      "    \"int\": 2,\n",
      "    \"mix\": null,\n",
      "    \"str\": \"foo\",\n",
      "    \"time\": \"2017-12-23 12:34:51+0900\"\n",
      "},\n",
      "{\n",
      "    \"bool\": true,\n",
      "    \"float\": 0,\n",
      "    \"int\": 3,\n",
      "    \"mix\": \"Infinity\",\n",
      "    \"str\": \"bar\",\n",
      "    \"time\": \"2017-03-03 22:44:55+0900\"\n",
      "},\n",
      "{\n",
      "    \"bool\": \"FALSE\",\n",
      "    \"float\": -9.9,\n",
      "    \"int\": -10,\n",
      "    \"mix\": \"nan\",\n",
      "    \"str\": \"\",\n",
      "    \"time\": \"2017-01-01 00:00:00+0900\"\n",
      "}]\n",
      "\n"
     ]
    }
   ],
   "source": [
    "writer = pytablewriter.JsonTableWriter()\n",
    "# writer.table_name = \"Timezone\"\n",
    "writer.header_list = header_list\n",
    "writer.value_matrix = data\n",
    "\n",
    "writer.write_table()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{ \"example_table\" : [\n",
      "{\n",
      "    \"bool\": true,\n",
      "    \"float\": 0.1,\n",
      "    \"int\": 0,\n",
      "    \"mix\": 0,\n",
      "    \"str\": \"hoge\",\n",
      "    \"time\": \"2017-01-01 03:04:05+0900\"\n",
      "},\n",
      "{\n",
      "    \"bool\": false,\n",
      "    \"float\": -2.23,\n",
      "    \"int\": 2,\n",
      "    \"mix\": null,\n",
      "    \"str\": \"foo\",\n",
      "    \"time\": \"2017-12-23 12:34:51+0900\"\n",
      "},\n",
      "{\n",
      "    \"bool\": true,\n",
      "    \"float\": 0,\n",
      "    \"int\": 3,\n",
      "    \"mix\": \"Infinity\",\n",
      "    \"str\": \"bar\",\n",
      "    \"time\": \"2017-03-03 22:44:55+0900\"\n",
      "},\n",
      "{\n",
      "    \"bool\": \"FALSE\",\n",
      "    \"float\": -9.9,\n",
      "    \"int\": -10,\n",
      "    \"mix\": \"nan\",\n",
      "    \"str\": \"\",\n",
      "    \"time\": \"2017-01-01 00:00:00+0900\"\n",
      "}]}\n",
      "\n"
     ]
    }
   ],
   "source": [
    "writer = pytablewriter.JsonTableWriter()\n",
    "writer.table_name = table_name\n",
    "writer.header_list = header_list\n",
    "writer.value_matrix = data\n",
    "\n",
    "writer.write_table()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\\begin{equation}\n",
      "    A = \\left( \\begin{array}{rrr}\n",
      "        0.01 &      0.0012 & 0.000 \\\\\n",
      "        1.00 &     99.9000 & 0.010 \\\\\n",
      "        1.20 & 999999.1230 & 0.001 \\\\\n",
      "    \\end{array} \\right)\n",
      "\\end{equation}\n",
      "\n"
     ]
    }
   ],
   "source": [
    "writer = pytablewriter.LatexMatrixWriter()\n",
    "writer.table_name = \"A\"\n",
    "writer.value_matrix = [\n",
    "    [0.01, 0.00125, 0.0],\n",
    "    [1.0, 99.9, 0.01],\n",
    "    [1.2, 999999.123, 0.001],\n",
    "]\n",
    "writer.write_table()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "\\begin{equation}\n",
    "    A = \\left( \\begin{array}{rrr}\n",
    "        0.01 &      0.0012 & 0.000 \\\\\n",
    "        1.00 &     99.9000 & 0.010 \\\\\n",
    "        1.20 & 999999.1230 & 0.001 \\\\\n",
    "    \\end{array} \\right)\n",
    "\\end{equation}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\\begin{equation}\n",
      "    B = \\left( \\begin{array}{llll}\n",
      "        a_{11} & a_{12} & \\ldots & a_{1n} \\\\\n",
      "        a_{21} & a_{22} & \\ldots & a_{2n} \\\\\n",
      "        \\vdots & \\vdots & \\ddots & \\vdots \\\\\n",
      "        a_{n1} & a_{n2} & \\ldots & a_{nn} \\\\\n",
      "    \\end{array} \\right)\n",
      "\\end{equation}\n",
      "\n"
     ]
    }
   ],
   "source": [
    "writer = pytablewriter.LatexMatrixWriter()\n",
    "writer.table_name = \"B\"\n",
    "writer.value_matrix = [\n",
    "    [\"a_{11}\", \"a_{12}\", \"\\\\ldots\", \"a_{1n}\"],\n",
    "    [\"a_{21}\", \"a_{22}\", \"\\\\ldots\", \"a_{2n}\"],\n",
    "    [r\"\\vdots\", \"\\\\vdots\", \"\\\\ddots\", \"\\\\vdots\"],\n",
    "    [\"a_{n1}\", \"a_{n2}\", \"\\\\ldots\", \"a_{nn}\"],\n",
    "]\n",
    "writer.write_table()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "\\begin{equation}\n",
    "    B = \\left( \\begin{array}{llll}\n",
    "        a_{11} & a_{12} & \\ldots & a_{1n} \\\\\n",
    "        a_{21} & a_{22} & \\ldots & a_{2n} \\\\\n",
    "        \\vdots & \\vdots & \\ddots & \\vdots \\\\\n",
    "        a_{n1} & a_{n2} & \\ldots & a_{nn} \\\\\n",
    "    \\end{array} \\right)\n",
    "\\end{equation}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\\begin{array}{r | r | l | l | l | l} \\hline\n",
      "    \\verb|int| & \\verb|float| & \\verb|str | & \\verb|bool | & \\verb| mix  | & \\verb|          time          | \\\\ \\hline\n",
      "    \\hline\n",
      "      0 &  0.10 & hoge & True  &      0 & \\verb|2017-01-01 03:04:05+0900| \\\\ \\hline\n",
      "      2 & -2.23 & foo  & False &        & \\verb|2017-12-23 12:34:51+0900| \\\\ \\hline\n",
      "      3 &  0.00 & bar  & True  & \\infty & \\verb|2017-03-03 22:44:55+0900| \\\\ \\hline\n",
      "    -10 & -9.90 &      & False & NaN    & \\verb|2017-01-01 00:00:00+0900| \\\\ \\hline\n",
      "\\end{array}\n",
      "\n"
     ]
    }
   ],
   "source": [
    "writer = pytablewriter.LatexTableWriter()\n",
    "writer.header_list = header_list\n",
    "writer.value_matrix = data\n",
    "\n",
    "writer.write_table()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "\\begin{array}{r | r | l | l | l | l} \\hline\n",
    "    \\verb|int| & \\verb|float| & \\verb|str| & \\verb|bool| & \\verb|mix| & \\verb|time| \\\\ \\hline\n",
    "    \\hline\n",
    "    0 & 0.10 & hoge & True & 0 & \\verb|2017-01-01 03:04:05+0900| \\\\ \\hline\n",
    "    2 & -2.23 & foo & False &  & \\verb|2017-12-23 12:34:51+0900| \\\\ \\hline\n",
    "    3 & 0.00 & bar & True & \\infty & \\verb|2017-03-03 22:44:55+0900| \\\\ \\hline\n",
    "    -10 & -9.90 &  & False & NaN & \\verb|2017-01-01 00:00:00+0900| \\\\ \\hline\n",
    "\\end{array}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "# example_table\n",
      "|int|float|str |bool |  mix   |          time          |\n",
      "|--:|----:|----|-----|-------:|------------------------|\n",
      "|  0| 0.10|hoge|True |       0|2017-01-01 03:04:05+0900|\n",
      "|  2|-2.23|foo |False|        |2017-12-23 12:34:51+0900|\n",
      "|  3| 0.00|bar |True |Infinity|2017-03-03 22:44:55+0900|\n",
      "|-10|-9.90|    |False|     NaN|2017-01-01 00:00:00+0900|\n",
      "\n"
     ]
    }
   ],
   "source": [
    "writer = pytablewriter.MarkdownTableWriter()\n",
    "writer.table_name = table_name\n",
    "writer.header_list = header_list\n",
    "writer.value_matrix = data\n",
    "\n",
    "writer.write_table()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "# write example with a margin\n",
      "| int | float | str  | bool  |   mix    |           time           |\n",
      "|----:|------:|------|-------|---------:|--------------------------|\n",
      "|   0 |  0.10 | hoge | True  |        0 | 2017-01-01 03:04:05+0900 |\n",
      "|   2 | -2.23 | foo  | False |          | 2017-12-23 12:34:51+0900 |\n",
      "|   3 |  0.00 | bar  | True  | Infinity | 2017-03-03 22:44:55+0900 |\n",
      "| -10 | -9.90 |      | False |      NaN | 2017-01-01 00:00:00+0900 |\n",
      "\n"
     ]
    }
   ],
   "source": [
    "writer = pytablewriter.MarkdownTableWriter()\n",
    "writer.table_name = \"write example with a margin\"\n",
    "writer.header_list = header_list\n",
    "writer.value_matrix = data\n",
    "writer.margin = 1  # add a whitespace for both sides of each cell\n",
    "\n",
    "writer.write_table()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{| class=\"wikitable\"\n",
      "|+example_table\n",
      "! int\n",
      "! float\n",
      "! str\n",
      "! bool\n",
      "! mix\n",
      "! time\n",
      "|-\n",
      "| style=\"text-align:right\"| 0\n",
      "| style=\"text-align:right\"| 0.10\n",
      "| hoge\n",
      "| True\n",
      "| style=\"text-align:right\"| 0\n",
      "| 2017-01-01 03:04:05+0900\n",
      "|-\n",
      "| style=\"text-align:right\"| 2\n",
      "| style=\"text-align:right\"| -2.23\n",
      "| foo\n",
      "| False\n",
      "| \n",
      "| 2017-12-23 12:34:51+0900\n",
      "|-\n",
      "| style=\"text-align:right\"| 3\n",
      "| style=\"text-align:right\"| 0.00\n",
      "| bar\n",
      "| True\n",
      "| Infinity\n",
      "| 2017-03-03 22:44:55+0900\n",
      "|-\n",
      "| style=\"text-align:right\"| -10\n",
      "| style=\"text-align:right\"| -9.90\n",
      "| \n",
      "| False\n",
      "| NaN\n",
      "| 2017-01-01 00:00:00+0900\n",
      "|}\n",
      "\n"
     ]
    }
   ],
   "source": [
    "writer = pytablewriter.MediaWikiTableWriter()\n",
    "writer.table_name = table_name\n",
    "writer.header_list = header_list\n",
    "writer.value_matrix = data\n",
    "\n",
    "writer.write_table()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "example_table = np.array([\n",
      "    [\"int\", \"float\", \"str\", \"bool\", \"mix\", \"time\"],\n",
      "    [0, 0.1, \"hoge\", True, 0, \"2017-01-01 03:04:05+0900\"],\n",
      "    [2, -2.23, \"foo\", False, None, \"2017-12-23 12:34:51+0900\"],\n",
      "    [3, 0, \"bar\", True, np.inf, \"2017-03-03 22:44:55+0900\"],\n",
      "    [-10, -9.9, \"\", False, np.nan, \"2017-01-01 00:00:00+0900\"],\n",
      "])\n",
      "\n"
     ]
    }
   ],
   "source": [
    "writer = pytablewriter.NumpyTableWriter()\n",
    "writer.table_name = table_name\n",
    "writer.header_list = header_list\n",
    "writer.value_matrix = data\n",
    "\n",
    "writer.write_table()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "example_table = pd.DataFrame([\n",
      "    [0, 0.1, \"hoge\", True, 0, \"2017-01-01 03:04:05+0900\"],\n",
      "    [2, -2.23, \"foo\", False, None, \"2017-12-23 12:34:51+0900\"],\n",
      "    [3, 0, \"bar\", True, np.inf, \"2017-03-03 22:44:55+0900\"],\n",
      "    [-10, -9.9, \"\", False, np.nan, \"2017-01-01 00:00:00+0900\"],\n",
      "], columns=[\"int\", \"float\", \"str\", \"bool\", \"mix\", \"time\"])\n",
      "\n"
     ]
    }
   ],
   "source": [
    "writer = pytablewriter.PandasDataFrameWriter()\n",
    "writer.table_name = table_name\n",
    "writer.header_list = header_list\n",
    "writer.value_matrix = data\n",
    "\n",
    "writer.write_table()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "example_table = pd.DataFrame([\n",
      "    [0, 0.1, \"hoge\", True, 0, \"2017-01-01 03:04:05+0900\"],\n",
      "    [2, -2.23, \"foo\", False, None, \"2017-12-23 12:34:51+0900\"],\n",
      "    [3, 0, \"bar\", True, np.inf, \"2017-03-03 22:44:55+0900\"],\n",
      "    [-10, -9.9, \"\", False, np.nan, \"2017-01-01 00:00:00+0900\"],\n",
      "], columns=[\"int\", \"float\", \"str\", \"bool\", \"mix\", \"time\"])\n",
      "\n"
     ]
    }
   ],
   "source": [
    "writer = pytablewriter.PandasDataFrameWriter()\n",
    "writer.table_name = table_name\n",
    "writer.header_list = header_list\n",
    "writer.value_matrix = data\n",
    "writer.is_datetime_instance_formatting = False\n",
    "\n",
    "writer.write_table()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "example_table = [\n",
      "    [\"int\", \"float\", \"str\", \"bool\", \"mix\", \"time\"],\n",
      "    [0, 0.1, \"hoge\", True, 0, \"2017-01-01 03:04:05+0900\"],\n",
      "    [2, -2.23, \"foo\", False, None, \"2017-12-23 12:34:51+0900\"],\n",
      "    [3, 0, \"bar\", True, float(\"inf\"), \"2017-03-03 22:44:55+0900\"],\n",
      "    [-10, -9.9, \"\", False, float(\"nan\"), \"2017-01-01 00:00:00+0900\"],\n",
      "]\n",
      "\n"
     ]
    }
   ],
   "source": [
    "writer = pytablewriter.PythonCodeTableWriter()\n",
    "writer.table_name = table_name\n",
    "writer.header_list = header_list\n",
    "writer.value_matrix = data\n",
    "\n",
    "writer.write_table()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "example_table = [\n",
      "    [\"int\", \"float\", \"str\", \"bool\", \"mix\", \"time\"],\n",
      "    [0, 0.1, \"hoge\", True, 0, \"2017-01-01 03:04:05+0900\"],\n",
      "    [2, -2.23, \"foo\", False, None, \"2017-12-23 12:34:51+0900\"],\n",
      "    [3, 0, \"bar\", True, float(\"inf\"), \"2017-03-03 22:44:55+0900\"],\n",
      "    [-10, -9.9, \"\", False, float(\"nan\"), \"2017-01-01 00:00:00+0900\"],\n",
      "]\n",
      "\n"
     ]
    }
   ],
   "source": [
    "writer = pytablewriter.PythonCodeTableWriter()\n",
    "writer.table_name = table_name\n",
    "writer.header_list = header_list\n",
    "writer.value_matrix = data\n",
    "writer.is_datetime_instance_formatting = False\n",
    "\n",
    "writer.write_table()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {
    "scrolled": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      ".. table:: example_table\n",
      "\n",
      "    +---+-----+----+-----+--------+------------------------+\n",
      "    |int|float|str |bool |  mix   |          time          |\n",
      "    +===+=====+====+=====+========+========================+\n",
      "    |  0| 0.10|hoge|True |       0|2017-01-01 03:04:05+0900|\n",
      "    +---+-----+----+-----+--------+------------------------+\n",
      "    |  2|-2.23|foo |False|        |2017-12-23 12:34:51+0900|\n",
      "    +---+-----+----+-----+--------+------------------------+\n",
      "    |  3| 0.00|bar |True |Infinity|2017-03-03 22:44:55+0900|\n",
      "    +---+-----+----+-----+--------+------------------------+\n",
      "    |-10|-9.90|    |False|     NaN|2017-01-01 00:00:00+0900|\n",
      "    +---+-----+----+-----+--------+------------------------+\n",
      "\n"
     ]
    }
   ],
   "source": [
    "writer = pytablewriter.RstGridTableWriter()\n",
    "writer.table_name = table_name\n",
    "writer.header_list = header_list\n",
    "writer.value_matrix = data\n",
    "\n",
    "writer.write_table()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      ".. table:: example_table\n",
      "\n",
      "    ===  =====  ====  =====  ========  ========================\n",
      "    int  float  str   bool     mix               time          \n",
      "    ===  =====  ====  =====  ========  ========================\n",
      "      0   0.10  hoge  True          0  2017-01-01 03:04:05+0900\n",
      "      2  -2.23  foo   False            2017-12-23 12:34:51+0900\n",
      "      3   0.00  bar   True   Infinity  2017-03-03 22:44:55+0900\n",
      "    -10  -9.90        False       NaN  2017-01-01 00:00:00+0900\n",
      "    ===  =====  ====  =====  ========  ========================\n",
      "\n"
     ]
    }
   ],
   "source": [
    "writer = pytablewriter.RstSimpleTableWriter()\n",
    "writer.table_name = table_name\n",
    "writer.header_list = header_list\n",
    "writer.value_matrix = data\n",
    "\n",
    "writer.write_table()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      ".. csv-table:: example_table\n",
      "    :header: \"int\", \"float\", \"str\", \"bool\", \"mix\", \"time\"\n",
      "    :widths: 5, 7, 6, 6, 8, 26\n",
      "\n",
      "    0, 0.10, \"hoge\", True, 0, \"2017-01-01 03:04:05+0900\"\n",
      "    2, -2.23, \"foo\", False, , \"2017-12-23 12:34:51+0900\"\n",
      "    3, 0.00, \"bar\", True, Infinity, \"2017-03-03 22:44:55+0900\"\n",
      "    -10, -9.90, , False, NaN, \"2017-01-01 00:00:00+0900\"\n",
      "\n"
     ]
    }
   ],
   "source": [
    "writer = pytablewriter.RstCsvTableWriter()\n",
    "writer.table_name = table_name\n",
    "writer.header_list = header_list\n",
    "writer.value_matrix = data\n",
    "\n",
    "writer.write_table()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "int:0\tfloat:0.1\tstr:\"hoge\"\tbool:True\tmix:0\ttime:\"2017-01-01 03:04:05+0900\"\n",
      "int:2\tfloat:-2.23\tstr:\"foo\"\tbool:False\ttime:\"2017-12-23 12:34:51+0900\"\n",
      "int:3\tfloat:0\tstr:\"bar\"\tbool:True\tmix:Infinity\ttime:\"2017-03-03 22:44:55+0900\"\n",
      "int:-10\tfloat:-9.9\tbool:False\tmix:NaN\ttime:\"2017-01-01 00:00:00+0900\"\n"
     ]
    }
   ],
   "source": [
    "writer = pytablewriter.LtsvTableWriter()\n",
    "writer.header_list = header_list\n",
    "writer.value_matrix = data\n",
    "\n",
    "writer.write_table()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[example_table]]\n",
      "int = 0\n",
      "float = 0.1\n",
      "str = \"hoge\"\n",
      "bool = true\n",
      "mix = 0\n",
      "time = \"2017-01-01 03:04:05+0900\"\n",
      "\n",
      "[[example_table]]\n",
      "int = 2\n",
      "float = -2.23\n",
      "str = \"foo\"\n",
      "bool = false\n",
      "time = \"2017-12-23 12:34:51+0900\"\n",
      "\n",
      "[[example_table]]\n",
      "int = 3\n",
      "float = 0\n",
      "str = \"bar\"\n",
      "bool = true\n",
      "mix = Infinity\n",
      "time = \"2017-03-03 22:44:55+0900\"\n",
      "\n",
      "[[example_table]]\n",
      "int = -10\n",
      "float = -9.9\n",
      "str = \"\"\n",
      "bool = false\n",
      "mix = NaN\n",
      "time = \"2017-01-01 00:00:00+0900\"\n",
      "\n"
     ]
    }
   ],
   "source": [
    "writer = pytablewriter.TomlTableWriter()\n",
    "writer.table_name = table_name\n",
    "writer.header_list = header_list\n",
    "writer.value_matrix = data\n",
    "\n",
    "\n",
    "writer.write_table()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "// without type hints:  column data types detected automatically by default\n",
      "const without_type_hint = [\n",
      "    [\"header_a\", \"header_b\", \"header_c\"],\n",
      "    [-1.1, \"2017-01-02 03:04:05\", new Date(\"2017-01-02T03:04:05\")],\n",
      "    [0.12, \"2017-02-03 04:05:06\", new Date(\"2017-02-03T04:05:06\")]\n",
      "];\n",
      "\n",
      "// with type hints: Integer, DateTime, String\n",
      "const with_type_hint = [\n",
      "    [\"header_a\", \"header_b\", \"header_c\"],\n",
      "    [-1, new Date(\"2017-01-02T03:04:05\"), \"2017-01-02 03:04:05\"],\n",
      "    [0, new Date(\"2017-02-03T04:05:06\"), \"2017-02-03 04:05:06\"]\n",
      "];\n",
      "\n"
     ]
    }
   ],
   "source": [
    "from datetime import datetime\n",
    "import pytablewriter as ptw\n",
    "\n",
    "writer = ptw.JavaScriptTableWriter()\n",
    "writer.header_list = [\"header_a\", \"header_b\", \"header_c\"]\n",
    "writer.value_matrix = [\n",
    "    [-1.1, \"2017-01-02 03:04:05\", datetime(2017, 1, 2, 3, 4, 5)],\n",
    "    [0.12, \"2017-02-03 04:05:06\", datetime(2017, 2, 3, 4, 5, 6)],\n",
    "]\n",
    "\n",
    "print(\"// without type hints:  column data types detected automatically by default\")\n",
    "writer.table_name = \"without type hint\"\n",
    "writer.write_table()\n",
    "\n",
    "print(\"// with type hints: Integer, DateTime, String\")\n",
    "writer.table_name = \"with type hint\"\n",
    "writer.type_hint_list = [ptw.Integer, ptw.DateTime, ptw.String]\n",
    "writer.write_table()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "python_variable_without_type_hints = [\n",
      "    [\"float\", \"infnan\", \"string\", \"datetime\"],\n",
      "    [-1.1, float(\"inf\"), \"2017-01-02 03:04:05\", dateutil.parser.parse(\"2017-01-02T03:04:05\")],\n",
      "    [0.12, float(\"nan\"), \"2017-02-03 04:05:06\", dateutil.parser.parse(\"2017-02-03T04:05:06\")],\n",
      "]\n",
      "\n",
      "python_variable_with_type_hints = [\n",
      "    [\"hint_int\", \"hint_str\", \"hint_datetime\", \"hint_str\"],\n",
      "    [-1, \"inf\", dateutil.parser.parse(\"2017-01-02T03:04:05\"), \"2017-01-02 03:04:05\"],\n",
      "    [0, \"nan\", dateutil.parser.parse(\"2017-02-03T04:05:06\"), \"2017-02-03 04:05:06\"],\n",
      "]\n",
      "\n"
     ]
    }
   ],
   "source": [
    "from datetime import datetime\n",
    "import pytablewriter as ptw\n",
    "\n",
    "writer = ptw.PythonCodeTableWriter()\n",
    "writer.value_matrix = [\n",
    "    [-1.1, float(\"inf\"), \"2017-01-02 03:04:05\", datetime(2017, 1, 2, 3, 4, 5)],\n",
    "    [0.12, float(\"nan\"), \"2017-02-03 04:05:06\", datetime(2017, 2, 3, 4, 5, 6)],\n",
    "]\n",
    "\n",
    "# column data types detected automatically by default\n",
    "writer.table_name = \"python variable without type hints\"\n",
    "writer.header_list = [\"float\", \"infnan\", \"string\", \"datetime\"]\n",
    "writer.write_table()\n",
    "\n",
    "# set type hints\n",
    "writer.table_name = \"python variable with type hints\"\n",
    "writer.header_list = [\"hint_int\", \"hint_str\", \"hint_datetime\", \"hint_str\"]\n",
    "writer.type_hint_list = [ptw.Integer, ptw.String, ptw.DateTime, ptw.String]\n",
    "writer.write_table()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "| i | f  | c  | if |ifc|bool |  inf   |nan|mix_num |          time           |\n",
      "|--:|---:|----|---:|---|-----|--------|---|-------:|-------------------------|\n",
      "|  1|1.10|aa  | 1.0|  1|True |Infinity|NaN|       1|2017-01-01 00:00:00+09:00|\n",
      "|  2|2.20|bbb | 2.2|2.2|False|Infinity|NaN|Infinity|2017-01-02 03:04:05+09:00|\n",
      "|  3|3.33|cccc|-3.0|ccc|True |Infinity|NaN|     NaN|2017-01-01 00:00:00+09:00|\n",
      "\n"
     ]
    }
   ],
   "source": [
    "writer = pytablewriter.MarkdownTableWriter()\n",
    "writer.from_csv(\n",
    "    dedent(\n",
    "        \"\"\"\\\n",
    "    \"i\",\"f\",\"c\",\"if\",\"ifc\",\"bool\",\"inf\",\"nan\",\"mix_num\",\"time\"\n",
    "    1,1.10,\"aa\",1.0,\"1\",True,Infinity,NaN,1,\"2017-01-01 00:00:00+09:00\"\n",
    "    2,2.20,\"bbb\",2.2,\"2.2\",False,Infinity,NaN,Infinity,\"2017-01-02 03:04:05+09:00\"\n",
    "    3,3.33,\"cccc\",-3.0,\"ccc\",True,Infinity,NaN,NaN,\"2017-01-01 00:00:00+09:00\"\n",
    "    \"\"\"\n",
    "    )\n",
    ")\n",
    "writer.write_table()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "# ps\n",
      "|USER|PID|%CPU|%MEM| VSZ |RSS |TTY|STAT|START|TIME|   COMMAND    |\n",
      "|----|--:|---:|---:|----:|---:|---|----|-----|----|--------------|\n",
      "|root|  1|   0| 0.4|77664|8784|?  |Ss  |May11|0:02|/sbin/init    |\n",
      "|root|  2|   0| 0.0|    0|   0|?  |S   |May11|0:00|[kthreadd]    |\n",
      "|root|  4|   0| 0.0|    0|   0|?  |I<  |May11|0:00|[kworker/0:0H]|\n",
      "|root|  6|   0| 0.0|    0|   0|?  |I<  |May11|0:00|[mm_percpu_wq]|\n",
      "|root|  7|   0| 0.0|    0|   0|?  |S   |May11|0:01|[ksoftirqd/0] |\n",
      "\n"
     ]
    }
   ],
   "source": [
    "writer = pytablewriter.MarkdownTableWriter()\n",
    "writer.table_name = \"ps\"\n",
    "writer.from_csv(\n",
    "    dedent(\n",
    "        \"\"\"\\\n",
    "        USER       PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND\n",
    "        root         1  0.0  0.4  77664  8784 ?        Ss   May11   0:02 /sbin/init\n",
    "        root         2  0.0  0.0      0     0 ?        S    May11   0:00 [kthreadd]\n",
    "        root         4  0.0  0.0      0     0 ?        I<   May11   0:00 [kworker/0:0H]\n",
    "        root         6  0.0  0.0      0     0 ?        I<   May11   0:00 [mm_percpu_wq]\n",
    "        root         7  0.0  0.0      0     0 ?        S    May11   0:01 [ksoftirqd/0]\n",
    "    \"\"\"\n",
    "    ),\n",
    "    delimiter=\" \",\n",
    ")\n",
    "writer.write_table()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}
